package com.cf.edu182;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

public class b {

    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static PrintWriter out = new PrintWriter(System.out);

    public static void main(String[] args) throws IOException {
        int T = Integer.parseInt(br.readLine().trim());
        while (T-- > 0) {
            solve();
        }
        out.flush();
        out.close();
        br.close();
    }

    static void solve() throws IOException {
        int n = Integer.parseInt(br.readLine().trim());
        String[] in = br.readLine().split(" ");
        int[] a = new int[n];

        Set<Integer> present = new HashSet<>(); // 只记录非零出现的数
        int zeroCount = 0;
        int zeroPos = -1;
        for (int i = 0; i < n; i++) {
            a[i] = Integer.parseInt(in[i]);
            if (a[i] == 0) {
                zeroCount++;
                zeroPos = i;
            } else {
                present.add(a[i]);
            }
        }

        // 从左找第一个 非“必然正确” 的位置 L
        int L = -1;
        for (int i = 0; i < n; i++) {
            boolean forcedCorrect = false;
            if (a[i] == i + 1) {
                // 非零且恰好在正确位置 -> 必然正确
                forcedCorrect = true;
            } else if (a[i] == 0 && zeroCount == 1 && zeroPos == i && !present.contains(i + 1)) {
                // 唯一的0，且必须填成 i+1 -> 必然正确
                forcedCorrect = true;
            }
            if (!forcedCorrect) {
                L = i;
                break;
            }
        }

        if (L == -1) { // 所有位置都是必然正确
            out.println(0);
            return;
        }

        // 从右找第一个 非“必然正确” 的位置 R
        int R = -1;
        for (int i = n - 1; i >= 0; i--) {
            boolean forcedCorrect = false;
            if (a[i] == i + 1) {
                forcedCorrect = true;
            } else if (a[i] == 0 && zeroCount == 1 && zeroPos == i && !present.contains(i + 1)) {
                forcedCorrect = true;
            }
            if (!forcedCorrect) {
                R = i;
                break;
            }
        }

        out.println(Math.max(0, R - L + 1));
    }
}
